package com.fancyy.scm.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.fancyy.scm.R;
import com.fancyy.scm.base.BaseActivity;
import com.fancyy.scm.base.annotation.Layout;
import com.fancyy.scm.base.annotation.Translucent;
import com.fancyy.scm.base.http.BRDataListener;
import com.fancyy.scm.base.http.BRDataTransport;
import com.fancyy.scm.databinding.ActivityCancelBinding;
import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import static com.fancyy.scm.BRConstant.CANCEL_QC_OR_PROCESSTRACKING;

@Translucent
@Layout(R.layout.activity_cancel)
public class CancelActivity extends BaseActivity<ActivityCancelBinding> {

    public String type; // Process  QC
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        type = getIntent().getStringExtra("type");

        setNeedScan(true);
        super.onCreate(savedInstanceState);
        if (type.equals("QC"))
            getDataBinding().title.setText("QC Cancel");
        else
            getDataBinding().title.setText("Process Cancel");
    }
    public void onScanningClick(View view) {
        if(!getPDA()) {
            Intent intent = new Intent(this, CaptureActivity.class);
            startActivityForResult(intent, 1);
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1 && resultCode == RESULT_OK) {
            String code = data.getStringExtra(CodeUtils.RESULT_STRING);
            handleCode(code);
        }
    }
    private void handleCode(String code) {
        getDataBinding().scan.setText(code);
        BRDataTransport.createPost(CANCEL_QC_OR_PROCESSTRACKING)
                .addParam("barCode",getDataBinding().scan.getText().toString())
                .addParam("ScanType", type)
                .addParam("UpdateBy", getBRApplication().getUserInfo().userAccount)
                .addProgressing(this, "Progressing...")
                .execute(new BRDataListener() {
                    @Override
                    public void onSuccess(Object data) {
                        showToast("Delete Success");
                    }
                    @Override
                    public void onFail(int status, String failMessage) {
                        if (failMessage!="")
                            showToast(failMessage);
                        else
                        showToast("The Carton is not exist or has been deleted");
                    }
                });
    }
    @Override
    public void doScan(String data) {
        handleCode(data);
//        closeScan();
    }
}
